package com.pkdl.dao;

import com.pkdl.dto.BangLuongDTO;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

// Data access object, which get data of BangLuong table from database
public class BangLuongDAO {
	public ArrayList<BangLuongDTO> selectAll() {
		ArrayList<BangLuongDTO> list = new ArrayList<BangLuongDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_SelectAll()}");
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				BangLuongDTO bangluong = new BangLuongDTO();
				bangluong.setMaLuong(rs.getInt("MaLuong"));
				bangluong.setThang(rs.getInt("Thang"));
				bangluong.setNam(rs.getInt("Nam"));
				bangluong.setMaNhanVien(rs.getInt("MaNhanVien"));
				bangluong.setSoNgayLam(rs.getInt("SoNgayLam"));
				bangluong.setLuongThucLanh(rs.getLong("LuongThucLanh"));
				list.add(bangluong);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

	public BangLuongDTO selectById(int maluong) {
		BangLuongDTO bangluong = new BangLuongDTO();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_SelectByID(?)}");
			cs.setInt(1, maluong);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				bangluong.setMaLuong(rs.getInt("MaLuong"));
				bangluong.setThang(rs.getInt("Thang"));
				bangluong.setNam(rs.getInt("Nam"));
				bangluong.setMaNhanVien(rs.getInt("MaNhanVien"));
				bangluong.setSoNgayLam(rs.getInt("SoNgayLam"));
				bangluong.setLuongThucLanh(rs.getLong("LuongThucLanh"));
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return bangluong;
	}

	public boolean insert(BangLuongDTO bangluong) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_Insert(?, ?, ?, ?, ?)}");
			cs.setInt(1, bangluong.getThang());
			cs.setInt(2, bangluong.getNam());
			cs.setInt(3, bangluong.getMaNhanVien());
			cs.setInt(4, bangluong.getSoNgayLam());
			cs.setLong(5, bangluong.getLuongThucLanh());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean update(BangLuongDTO bangluong) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_Update(?, ?, ?, ?, ?, ?)}");
			cs.setInt(1, bangluong.getMaLuong());
			cs.setInt(2, bangluong.getThang());
			cs.setInt(3, bangluong.getNam());
			cs.setInt(4, bangluong.getMaNhanVien());
			cs.setInt(5, bangluong.getSoNgayLam());
			cs.setLong(6, bangluong.getLuongThucLanh());
			int n = cs.executeUpdate();
			if (n == 0) {
				result =false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public boolean delete(int maluong) {
		boolean result = true;
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_Delete(?)}");
			cs.setInt(1, maluong);
			int n = cs.executeUpdate();
			if (n == 0) {
				result = false;
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return result;
	}

	public ArrayList<BangLuongDTO> selectByMaNhanVien(int manhanvien) {
		ArrayList<BangLuongDTO> list = new ArrayList<BangLuongDTO>();
		Connection connect = null;
		try {
			connect = DataAccessHelper.getConnection();
			CallableStatement cs = connect.prepareCall("{call usp_BangLuong_SelectByMaNhanVien(?)}");
			cs.setInt(1, manhanvien);
			ResultSet rs = cs.executeQuery();
			while (rs.next()) {
				BangLuongDTO bangluong = new BangLuongDTO();
				bangluong.setMaLuong(rs.getInt("MaLuong"));
				bangluong.setThang(rs.getInt("Thang"));
				bangluong.setNam(rs.getInt("Nam"));
				bangluong.setMaNhanVien(rs.getInt("MaNhanVien"));
				bangluong.setSoNgayLam(rs.getInt("SoNgayLam"));
				bangluong.setLuongThucLanh(rs.getLong("LuongThucLanh"));
				list.add(bangluong);
			}
			connect.close();
		} catch (SQLException ex) {
			Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
		} finally {
			try {
				connect.close();
			} catch (SQLException ex) {
				Logger.getLogger(BangLuongDAO.class.getName()).log(Level.SEVERE, null, ex);
			}
		}
		return list;
	}

}
